package org.client.iot.ui.device.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;

import androidx.lifecycle.Observer;

import com.clj.fastble.data.BleDevice;
import com.lucky.base.network.NetworkApi;
import com.lucky.base.network.observer.BaseObserver;
import com.lucky.utils.log.LogUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;

import org.client.iot.constant.GlobeDataManager;
import org.client.iot.databinding.FragmentDeviceInfoBinding;
import org.client.iot.domain.DeviceInfoDto;
import org.client.iot.http.ApiService;
import org.client.iot.http.BaseDevReq;
import org.client.iot.ui.ble.DemoActivity;
import org.tzw.template.base.BaseFragment;

import io.reactivex.Observable;

public class DeviceInfoFragment extends BaseFragment<FragmentDeviceInfoBinding> {
    private MiniLoadingDialog mMiniLoadingDialog;

    @Override
    protected void initData(View view) {

    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initListener() {
        mRoot.btnDemo.setOnClickListener(v -> {
            BleDevice device = GlobeDataManager.bleDeviceData.getValue();
            if (device == null) {
                showToastShort("请先连接设备!");
                return;
            }
            Intent intent = new Intent(requireActivity(), DemoActivity.class);
            intent.putExtra("targetDev", device);
            startActivity(intent);
        });
        GlobeDataManager.bleDeviceData.observe(this, new Observer<BleDevice>() {
            @Override
            public void onChanged(BleDevice device) {
//                mRoot.textViewDevName.setText(device.getName());
//                mRoot.textViewDevImei.setText(device.getMac());
            }
        });
        GlobeDataManager.bluetoothProfileLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer status) {
//                if(status == BluetoothProfile.STATE_CONNECTING){
//                    mRoot.textViewVersion.setText(R.string.common_connecting);
//                }else if(status == BluetoothProfile.STATE_CONNECTED){
//                    mRoot.textViewVersion.setText(R.string.common_connect_ok);
//                }else{
//                    mRoot.textViewVersion.setText(getString(R.string.common_connect_fail));
//                }
            }
        });
    }

    // 获取设备信息
    private void getDevInfo() {
        Observable<BaseDevReq<DeviceInfoDto>> compose = NetworkApi.createService(ApiService.class, "dev")
                .getDevInfo().compose(NetworkApi.applySchedulers(new BaseObserver<BaseDevReq<DeviceInfoDto>>() {
                    @SuppressLint({"CheckResult", "SetTextI18n"})
                    @Override
                    public void onSuccess(BaseDevReq<DeviceInfoDto> req) {
                        if (req == null) {
                            return;
                        }
                        requireActivity().runOnUiThread(() -> {
                            disLoading();
                            LogUtils.i("获取设备信息req==", req.toString());
                            if (req.getData() != null) {
                                mRoot.textViewDeviceName.setText(req.getData().getDeviceName() == null ? "" : req.getData().getDeviceName());
                                mRoot.textViewDeviceModel.setText(req.getData().getDeviceModel() == null ? "" : req.getData().getDeviceModel());
                                mRoot.textViewDeviceBatteryPercent.setText(req.getData().getDeviceBatteryPercent() + "%");
                                mRoot.textViewDeviceChargingStatus.setText(req.getData().getDeviceChargingStatus() == null ? "" : req.getData().getDeviceChargingStatus());
                                mRoot.textViewSensorBatteryPercent.setText(req.getData().getSensorBatteryPercent() + "%");
                                mRoot.textViewSensorChargingStatus.setText(req.getData().getSensorChargingStatus() == null ? "" : req.getData().getSensorChargingStatus());
                                mRoot.textViewWorkingMode.setText(req.getData().getWorkingMode() == null ? "" : req.getData().getWorkingMode());
                                mRoot.textViewBrakingStatus.setText(req.getData().getBrakingStatus() == null ? "" : req.getData().getBrakingStatus());
                                mRoot.textViewSensorCommunicationStatus.setText(req.getData().getSensorCommunicationStatus() == null ? "" : req.getData().getSensorCommunicationStatus());
                                mRoot.textViewNetworkAccessStatus.setText(req.getData().getNetworkAccessStatus() == null ? "" : req.getData().getNetworkAccessStatus());
                                mRoot.textViewMeasurementDirection.setText(req.getData().getMeasurementDirection() == null ? "" : req.getData().getMeasurementDirection());
                            }
                        });
                    }

                    @SuppressLint("CheckResult")
                    @Override
                    public void onFailure(Throwable e) {
                        requireActivity().runOnUiThread(() -> {
                                    disLoading();
                                    showToastLong(e.getMessage());
                                }
                        );
                    }

                }));
    }


    private void disLoading() {
        requireActivity().runOnUiThread(() -> {
            if (mMiniLoadingDialog != null) {
                mMiniLoadingDialog.dismiss();
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        disLoading();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(requireActivity(), "获取设备信息中...");
        mMiniLoadingDialog.show();
        getDevInfo();
    }
}